class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix) {
        int n = matrix.size();
        vector<vector<int>> dp(n + 1, vector<int>(n + 2, INT_MAX));
        for (int i = 0; i < n + 2; i++) dp[0][i] = 0;
        for (int i = 1; i < n + 1; i++)
            for (int j = 1; j < n + 1; j++)
                dp[i][j] = min(min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i - 1][j + 1]) + matrix[i - 1][j - 1];
        int ret = INT_MAX;
        for (int i = 0; i < n + 2; i++)
            ret = min(ret, dp[n][i]);
        return ret;
    }
};